home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 2 / Mac Magazin and MacEasy Magazine CD - Issue 02.iso / Sharewarebibliothek / Applikationen / Alpha.5.81 folder / Tcl / UserCode / Text Filters / Text Munging / wordcount < prev   
Text File  |  1993-10-19  |  665b  |  23 lines

  1. #!/usr/bin/perl
  2.  
  3. $/ = "";                        # Enable paragraph mode.
  4. $* = 1;                         # Enable multi-line patterns.
  5.  
  6. # Now read each paragraph and split into words.  Record each
  7. # instance of a word in the %wordcount associative array.
  8.  
  9. while (<>) {
  10.     s/-\n//g;                   # Dehyphenate hyphenations.
  11.     tr/A-Z/a-z/;                # Canonicalize to lower case.
  12.     @words = split(/\W*\s+\W*/, $_);
  13.     foreach $word (@words) {
  14.     $wordcount{$word}++;    # Increment the entry.
  15.     }
  16. }
  17.  
  18. # Now print out all the entries in the %wordcount array.
  19.  
  20. foreach $word (sort keys(%wordcount)) {
  21.     printf "%20s %d\n", $word, $wordcount{$word};
  22. }
  23.